package com.an.trip.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import com.an.common.excel.ExcelResult;
import com.an.common.utils.poi.ExcelUtil;
import com.an.trip.domain.bo.TripMemberBo;
import com.an.trip.domain.vo.TripMemberVo;
import com.an.trip.service.ITripMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 行程人员
 *
 * @author Ann
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trip/member")
public class TripMemberController extends BaseController {

    private final ITripMemberService tripMemberService;

    /**
     * 查询行程人员列表
     */
    @SaCheckPermission("trip:member:list")
    @GetMapping("/list")
    public TableDataInfo<TripMemberVo> list(TripMemberBo bo, PageQuery pageQuery) {
        return tripMemberService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询行程人员列表
     */
    @SaCheckPermission("trip:member:list")
    @GetMapping("/allMembers")
    public R<List<TripMemberVo>> allMembers() {
        return R.ok(tripMemberService.queryAllMembers());
    }

    /**
     * 导出行程人员列表
     */
    @SaCheckPermission("trip:member:export")
    @Log(title = "行程人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TripMemberBo bo, HttpServletResponse response) {
        List<TripMemberVo> list = tripMemberService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员", TripMemberVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @Log(title = "行程人员", businessType = BusinessType.IMPORT)
    @SaCheckPermission("trip:member:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<TripMemberVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), TripMemberVo.class, true);
        List<TripMemberVo> volist = excelResult.getList();
        tripMemberService.importData(volist);
        return R.ok(excelResult.getAnalysis());
    }

    /**
     * 获取行程人员详细信息
     *
     * @param memberId 主键
     */
    @SaCheckPermission("trip:member:query")
    @GetMapping("/{memberId}")
    public R<TripMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long memberId) {
        return R.ok(tripMemberService.queryById(memberId));
    }

    /**
     * 新增行程人员
     */
    @SaCheckPermission("trip:member:add")
    @Log(title = "行程人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TripMemberBo bo) {
        return toAjax(tripMemberService.insertByBo(bo));
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "人员数据", TripMemberVo.class, response);
    }

    /**
     * 修改行程人员
     */
    @SaCheckPermission("trip:member:edit")
    @Log(title = "行程人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TripMemberBo bo) {
        return toAjax(tripMemberService.updateByBo(bo));
    }

    /**
     * 删除行程人员
     *
     * @param memberIds 主键串
     */
    @SaCheckPermission("trip:member:remove")
    @Log(title = "行程人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] memberIds) {
        return toAjax(tripMemberService.deleteWithValidByIds(Arrays.asList(memberIds), true));
    }
}
